package nina.yiyou.salary.input.info

import nina.yiyou.salary.input.util.CellValueGetter
import nina.yiyou.salary.input.util.ExcelReadOpener
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

/**
 * 读取项目奖金表
 */
class BonusExcelReader {

  BonusExcelReader(String excelPath) {
    _excelPath = excelPath
  }

  Map read() {
    XSSFWorkbook workbook = new ExcelReadOpener(_excelPath).openXlsx()
    XSSFSheet sheet = workbook.getSheetAt(0)

    def result = range(2, sheet.lastRowNum - 1)
        .collect { sheet.getRow(it) }
        .collect { toLineMap(it) }
        .collectEntries { [it[COLUMN_LIST[0][0]], it] }

    workbook.close()
    return result
  }

  private List<Integer> range(int start, int end) {
    return (start <= end) ? (start..end) : []
  }

  private Map toLineMap(XSSFRow row) {
    return COLUMN_LIST.collectEntries { String key, int col ->
      [key, new CellValueGetter(row.getCell(col)).get()]
    }
  }

  private static final List<List> COLUMN_LIST = [
      ['工号', 0],
      ['实发', 12],
  ]

  private final String _excelPath
}
